﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using ARS.Control.Custom.Entity.Attributes;
using ARS.PublicTool;

namespace ARS.Control.Common.EntityManage
{
    public abstract class EntityTableInfoBase
    {
        private Type _typeModel;
        public Type typeModel
        {
            get
            {
                if (_typeModel == null)
                {
                    //if (Entity == null)
                    //{
                    //    return null;
                    //    //throw new Exception("aaa");
                    //    //throw new HttpException(404, "Not Found"); 
                    //}
                    _typeModel = Model.GetType();
                }
                return _typeModel;
            }
            set
            {
                _typeModel = value;
            }
        }

        //public Type TypeModel
        //{
        //    get;
        //    set;
        //}

        public object Model
        {
            get;
            set;
        }

        //protected int _type = 1;

        public int Type;

        protected string tableName;
        protected int strategy;
        protected PropertyInfo[] properties;

        /// <summary>
        /// 数据库存储表名
        /// </summary>
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        /// <summary>
        /// 策略
        /// </summary>
        //public int Strategy
        //{
        //    get { return strategy; }
        //    set { strategy = value; }
        //}
        /// <summary>
        /// 主键
        /// </summary>
        public EntityKeyInfo KeyInfo
        {
            get;
            set;
        }





        public void Create(object model)
        {
            if (model != null)
            {
                this.KeyInfo = new EntityKeyInfo();
                this.typeModel = model.GetType();
                this.Model = model;
                tableName = this.GetTableName(typeModel);
                //string html_Control_Name = null;
                properties = ReflectionHelper.GetProperties(typeModel);

                this.ItemLoop(typeModel, model);
            }

        }

        /// <summary>
        /// 遍历对象
        /// </summary>
        public abstract void ItemLoop(Type typeModel, object model);




        /// <summary>
        /// 获取表名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected string GetTableName(Type type)
        {
            string strTableName = string.Empty;
            object[] classAttr = type.GetCustomAttributes(false);
            if (classAttr.Length > 0)
            {
                foreach (var attr in classAttr)
                {
                    if (attr is ClassTableInfo)
                    {
                        ClassTableInfo tableAttr = attr as ClassTableInfo;
                        strTableName = tableAttr.Db_Table_Name;
                        break;
                    }
                }
                if (string.IsNullOrEmpty(strTableName))
                {
                    strTableName = type.Name;

                }
            }
            else
            {
                strTableName = type.Name;
            }
            return strTableName;
        }

        private ClassTableInfo _classTableInfo;

        public ClassTableInfo GetClassTableInfo()
        {
            if (this._classTableInfo == null)
            {

                object[] classAttr = this.typeModel.GetCustomAttributes(false);
                if (classAttr.Length > 0)
                {
                    foreach (var attr in classAttr)
                    {
                        if (attr is ClassTableInfo)
                        {
                            ClassTableInfo tableAttr = attr as ClassTableInfo;
                            //_classTableInfo.Db_Table_Name = tableAttr.Db_Table_Name;
                            this._classTableInfo = tableAttr;
                            break;
                        }
                    }
                    if (this._classTableInfo == null)
                    {
                        this._classTableInfo = new ClassTableInfo();
                    }
                    if (string.IsNullOrEmpty(_classTableInfo.Db_Table_Name))
                    {
                        _classTableInfo.Db_Table_Name = this.typeModel.Name;

                    }
                }
                else
                {
                    this._classTableInfo = new ClassTableInfo();
                    _classTableInfo.Db_Table_Name = this.typeModel.Name;
                }
            }

            return this._classTableInfo;
        }

    }
}
